package com.ygl.config.core.configuration;

import com.ygl.config.core.initializer.ConfigInitializer;
import com.ygl.config.core.parser.CoreConfigParser;
import com.ygl.config.core.parser.xml.XmlConfigParser;
import com.ygl.core.context.CoreContextAware;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * author: yanggld
 * date: 2019/12/15 21:34
 * version: 1.0
 * description: ConfigAutoConfiguration
 */
@Configuration
public class ConfigAutoConfiguration {

    @Autowired
    private CoreContextAware coreContextAware;

    @Bean
    public ConfigInitializer configInitializer() {
        return new ConfigInitializer();
    }

    @Bean
    public CoreConfigParser coreConfigParser() {
        String configFormat = coreContextAware.getConfigFormat();
        if (StringUtils.equals(configFormat, "xml")) {
            return new XmlConfigParser();
        }
        throw new RuntimeException("Invalid config format for '" + configFormat + "'");
    }
}
